package com.kancy.delay.queue.redis.message;

import java.time.Duration;
import java.util.Objects;
import java.util.UUID;

/**
 * <p>
 * DelayMessage
 * <p>
 *
 * @author: kancy
 * @date: 2020/3/4 11:03
 **/
public class DelayMessage<T> {
    public static final String TRACE_ID_KEY = "X-B3-TraceId";

    public DelayMessage() {
        setId(UUID.randomUUID().toString());
        setCreateTime(System.currentTimeMillis());
    }

    public DelayMessage(String topic, T payload, Duration delay) {
        this();
        this.topic = topic;
        this.payload = payload;
        this.delay = delay.toMillis();
    }

    public DelayMessage(String topic, T payload, long delaySeconds) {
        this();
        this.topic = topic;
        this.payload = payload;
        this.delay = delaySeconds * 1000;
    }

    /**
     * 消息唯一id
     */
    private String id;
    /**
     * 主题
     */
    private String topic;
    /**
     * 重试次数
     */
    private Integer retry;
    /**
     * 延时时间,单位毫秒
     */
    private Long delay;
    /**
     * 创建时间
     */
    private Long createTime;

    /**
     * traceId
     */
    private String traceId;
    /**
     * 数据体
     */
    private T payload;

    public String getId() {
        return id;
    }

    private void setId(String id) {
        this.id = id;
    }

    public String getTopic() {
        return topic;
    }

    public void setTopic(String topic) {
        this.topic = topic;
    }

    public Integer getRetry() {
        return Objects.isNull(retry) ? 0 : retry;
    }

    public void setRetry(Integer retry) {
        this.retry = retry;
    }

    public Long getDelay() {
        return delay;
    }

    public void setDelay(Long delay) {
        this.delay = delay;
    }

    public T getPayload() {
        return payload;
    }

    public void setPayload(T payload) {
        this.payload = payload;
    }

    public Long getCreateTime() {
        return createTime;
    }

    private void setCreateTime(Long createTime) {
        this.createTime = createTime;
    }

    public String getTraceId() {
        return traceId;
    }

    public void setTraceId(String traceId) {
        this.traceId = traceId;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("{")
                    .append("id='").append(getId()).append("'").append(", ")
                    .append("topic='").append(getTopic()).append("'").append(", ")
                    .append("delay='").append(getDelay()).append("'").append(", ")
                    .append("retry='").append(getRetry()).append("'").append(", ")
                    .append("payload=").append(getPayload())
                .append("}");
        return builder.toString();
    }
}
